package cn.huiyunche.admin.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.huiyunche.base.service.interfaces.BVehicleService;
import cn.huiyunche.base.service.model.BVehicle;
import cn.huiyunche.base.service.vo.BVehicleBrandVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;

@Controller
@RequestMapping("/vehicle")
public class VehicleController {

	@Autowired
	private BVehicleService bVehicleService = null;

	private BVehicleService getBVehicleService() {
		return this.bVehicleService;
	}

	/**
	 * 车型信息维护列表
	 * 
	 * @param pageVo
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<Object> list(PageVo pageVo, BVehicleBrandVo vo) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "查询成功");
		result.setData(this.getBVehicleService().selectVehicleByParams(pageVo, vo));
		return result;
	}

	/**
	 * 更新或修改车型数据
	 * 
	 * @param bVehicle
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public Result<Object> modify(BVehicle bVehicle) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "车系数据增加成功");
		Long id = bVehicle.getId();
		if(id != null){
			Long brandId = bVehicle.getBrandId();
			String vehicleName = bVehicle.getVehicleName();
			if(0==brandId||null==brandId||StringUtils.isBlank(vehicleName)){
				result.setSuccess(false);
				result.setMessage("车系必填参数为空");
				return result;
			}
			result.setMessage("数据更新已成功");
		}
		result.setData(this.getBVehicleService().modify(bVehicle));
		return result;
	}
	
	/**
	 * 
	 * 根据品牌ID查询车系
	 * 
	 * @param brandId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/vehicle", method = RequestMethod.GET)
	public Result<Object> selectVehiclesByBrandId(Long brandId) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "查询数据成功");
		result.setData(this.getBVehicleService().loadVehicle(brandId));
		return result;
	}
	
	/**
	 * 
	 * 查询所有品牌
	 * 
	 * @param brandId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/brand", method = RequestMethod.GET)
	public Result<Object> loadBrand() throws Exception {
		Result<Object> result = new Result<Object>(true, null, "查询数据成功");
		result.setData(this.getBVehicleService().loadBrand());
		return result;
	}

}
